package com.mingchuang.administrator.mingchuang.pile;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.exception.DbException;
import com.mingchuang.administrator.mingchuang.AppApplication;
import com.mingchuang.administrator.mingchuang.GetLocationActivity;
import com.mingchuang.administrator.mingchuang.R;
import com.mingchuang.administrator.mingchuang.bean.PileInfoBean;
import com.mingchuang.administrator.mingchuang.utils.Constant;
import com.mingchuang.administrator.mingchuang.utils.Util;
import com.mingchuang.administrator.mingchuang.zip.ZipUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * Created by Administrator on 2016/1/19.
 */
public class PileInfoActivity extends AppCompatActivity implements View.OnClickListener {

    private PileInfoBean pileInfo;

    /**
     * private String projectName;
     * private String pileName;
     * private String testLen;
     * private String sectionNum;
     * private String section;
     * private String testDate;
     * private String uploadDate;
     * <p/>
     * <p/>
     * private String isUpload = "0";//0未上传   1上传
     * private String isPic = "0";//是否有照片
     * private String isGps = "0";
     * private String path;
     * <p/>
     * <p/>
     * private String pic1;
     * private String pic2;
     * private String pic3;
     */

    private TextView proName;
    private TextView pileName;
    private TextView testLen;
    private TextView sectionNum;
    private TextView testDate;
    private TextView uploadDate;

    private Button getGpsBtn;
    private Button removeGps;
    private Button getPic;
    private Button removePic;
    private Button upLoadBtn;

    private LinearLayout poContent ;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pile_info_activity);


        Intent intent = getIntent();
        if (intent != null) {
            pileInfo = (PileInfoBean) intent.getSerializableExtra("pileInfo");
        } else {
            //传值错误
            finish();
        }

        initView();
    }

    private void initView() {

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("基桩信息");
        proName = (TextView) findViewById(R.id.proj_name);
        pileName = (TextView) findViewById(R.id.pile_name);
        testLen = (TextView) findViewById(R.id.testLen);
        sectionNum = (TextView) findViewById(R.id.po_num);
        testDate = (TextView) findViewById(R.id.test_time);
        uploadDate = (TextView) findViewById(R.id.upload_time);

        getGpsBtn = (Button) findViewById(R.id.get_gps_btn);
        removeGps = (Button) findViewById(R.id.delete_gps_btn);
        removePic = (Button) findViewById(R.id.delete_photo_btn);
        getPic = (Button) findViewById(R.id.take_photo_btn);

        upLoadBtn = (Button) findViewById(R.id.upload_btn);

        poContent = (LinearLayout) findViewById(R.id.selection_container);


        getGpsBtn.setOnClickListener(this);
        removeGps.setOnClickListener(this);
        removePic.setOnClickListener(this);
        getPic.setOnClickListener(this);

        upLoadBtn.setOnClickListener(this);


        if (pileInfo != null) {
            proName.setText(pileInfo.getProjectName());
            pileName.setText(pileInfo.getPileName());
            testLen.setText(pileInfo.getTestLen());
            sectionNum.setText(pileInfo.getSectionNum());
            testDate.setText(pileInfo.getTestDate());
            if (pileInfo.getIsUpload().equals("1")) {
                uploadDate.setText(pileInfo.getUploadDate());
            }
            String[] seltction = pileInfo.getSection().split(":");

            int i = Integer.parseInt(pileInfo.getSectionNum());
            for(int l = 0; l< i; l++){
                TextView tx = new TextView(this);
                tx.setText("剖面"+(l+1)+" : "+seltction[l]);
                poContent.addView(tx);
            }

        }


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;

            default:
                break;
        }
        return super.onOptionsItemSelected(item);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.get_gps_btn:
                //定位
                Intent intent = new Intent();
                intent.setClass(this, GetLocationActivity.class);
                intent.putExtra("path",pileInfo.getPath());
                intent.putExtra("id",pileInfo.getId());
                intent.putExtra("pileInfo",pileInfo);
                startActivity(intent);
                break;
            case R.id.delete_gps_btn:
                //删除gps xml  db
                File gpsFile = new File(pileInfo.getPath()+File.separator+"gps.xml");
                if(gpsFile.exists()){
                    if(gpsFile.delete()){
                        //删除成功
                        Toast.makeText(PileInfoActivity.this,"GPS删除成功",Toast.LENGTH_SHORT).show();
                        DbUtils db = AppApplication.getDb();
                        try {
                            String sql = "update PileInfoBean set isGps = '0'where id= "+pileInfo.getId();
                            db.execNonQuery(sql);
                        } catch (DbException e) {
                            e.printStackTrace();
                        }

                    }else{
                        Toast.makeText(PileInfoActivity.this,"GPS数据不存在",Toast.LENGTH_SHORT).show();
                    }

                }

                break;
            case R.id.delete_photo_btn:
                //删除图片 db




                break;
            case R.id.upload_btn:
                //文件上传

                //先压缩 后上传

                dialog = new ProgressDialog(PileInfoActivity.this);
                dialog.setTitle("开始压缩");
                dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                dialog.setCancelable(false);
                dialog.show();
                zipFile(pileInfo.getPath());

                break;
            case R.id.take_photo_btn:
                //拍照
                Intent intent2 = new Intent();
                intent2.setClass(this, com.mingchuang.administrator.mingchuang.photo.activity.MainActivity.class);
                intent2.putExtra("path",pileInfo.getPath());
                intent2.putExtra("id",pileInfo.getId());
                startActivity(intent2);

                break;


            default:

                break;




        }


    }

    ProgressDialog dialog ;

    public Handler mHandler = new Handler(){


        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //开始上传
            dialog.dismiss();
            Constant.upload(pileInfo.getPath() , PileInfoActivity.this ,pileInfo.getId() );


        }
    };


    public void zipFile(final String path ){
        ///storage/sdcard0/MC/unzip/玉环互通主线桥/右20-2
        Thread thread = new Thread(){
            @Override
            public void run() {
                try {
                    ZipUtil.zip("", "", path);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    mHandler.sendEmptyMessage(1);

                }

            }
        };
        thread.start();
    }
}
